//
//  OneScreenShieldView.m
//  OKP
//
//  Created by SY on 2025/6/19.
//

#import "OneScreenShieldView.h"

@interface OneScreenShieldView ()

@property (nonatomic, strong, nullable) UIView *safeZone;
@property(nonatomic, strong) UITextField *textField;

@end

@implementation OneScreenShieldView

+ (instancetype)createWithFrame:(CGRect)frame {
    return [[self alloc] initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.safeZone = [self makeSecureView];
        if (self.safeZone) {
            [self addSubview:self.safeZone];
            
            [self.safeZone setTranslatesAutoresizingMaskIntoConstraints:NO];
            
            [NSLayoutConstraint activateConstraints:@[
                [self.safeZone.topAnchor constraintEqualToAnchor:self.topAnchor constant:0],
                [self.safeZone.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0],
                [self.safeZone.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:0],
                [self.safeZone.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:0]
            ]];
            
            [self.safeZone setContentHuggingPriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisVertical];
            [self.safeZone setContentHuggingPriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisHorizontal];
            [self.safeZone setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh - 1 forAxis:UILayoutConstraintAxisVertical];
            [self.safeZone setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh - 1 forAxis:UILayoutConstraintAxisHorizontal];
        }
    }
    return self;
}

- (nullable instancetype)initWithCoder:(NSCoder *)coder {
    NSCAssert(NO, @"initWithCoder: has not been implemented");
    return nil;
}

- (void)addSubview:(UIView *)view {
    if (self.safeZone && view != self.safeZone) {
        [self.safeZone addSubview:view];
    } else {
        [super addSubview:view];
    }
}

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview {
    if (self.safeZone && view != self.safeZone) {
        [self.safeZone insertSubview:view belowSubview:siblingSubview];
    } else {
        [super insertSubview:view belowSubview:siblingSubview];
    }
}

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview {
    if (self.safeZone && view != self.safeZone) {
        [self.safeZone insertSubview:view aboveSubview:siblingSubview];
    } else {
        [super insertSubview:view aboveSubview:siblingSubview];
    }
}

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
    if (self.safeZone && view != self.safeZone) {
        [self.safeZone insertSubview:view atIndex:index];
    } else {
        [super insertSubview:view atIndex:index];
    }
}

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 {
    if (self.safeZone) {
        [self.safeZone exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
    } else {
        [super exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
    }
}

- (void)bringSubviewToFront:(UIView *)view {
    if (self.safeZone && view != self.safeZone) {
        [self.safeZone bringSubviewToFront:view];
    } else {
        [super bringSubviewToFront:view];
    }
}

- (void)sendSubviewToBack:(UIView *)view {
    if (self.safeZone && view != self.safeZone) {
        [self.safeZone sendSubviewToBack:view];
    } else {
        [super sendSubviewToBack:view];
    }
}

- (nullable UIView *)makeSecureView {
//    if ([self isOSVersionSafe]==YES) return nil;
    
    UITextField *field = [[UITextField alloc] init];
    field.secureTextEntry = YES;
    self.textField = field;
    
    UIView *fv = field.subviews.firstObject;
    for (UIView *subview in fv.subviews) {
        [subview removeFromSuperview];
    }
    fv.userInteractionEnabled = YES;
    
    NSString *errorMsg = @"[ScreenShieldView log] Create safeZone failed!";
    NSCAssert(fv, errorMsg); // In production, replace with logging or other error handling
    
    return fv;
}

- (BOOL)isOSVersionSafe {
    NSArray *unsafeOSVersions = @[@"15.1"];
    NSString *osVersion = [[UIDevice currentDevice] systemVersion];
    for (NSString *version in unsafeOSVersions) {
        if ([osVersion containsString:version]) {
            return NO;
        }
    }
    return YES;
}
- (void)changeScreenShotToOpen:(BOOL)isOpen{
    self.textField.secureTextEntry = isOpen?NO:YES;
}
@end
